package com.lowkey.sqlTools;

import java.util.ArrayList;
import java.util.List;

public class SQLTools {
	
	private static List<String> operationDic = new ArrayList<String>();
	static {
		operationDic.add("or");
		operationDic.add("and");
		operationDic.add("in");
	}
	
	public static SQLInfo getSQLInfo(String lql){
		SQLInfo info = new SQLInfo();
		lql=lql.replaceAll("\\s+", " ");
//		System.out.println(lql);
		String[] lqls=lql.split("where");
		String t =lqls[0].trim();
		String[] ts = t.split(" ");
		List<String> condition = null;
		List<String> operation = null;
		for(int i =0 ;i<ts.length;i++){
			if(ts[i].equalsIgnoreCase("from")){
				info.setTableName(ts[i+1]);
				break;
			}
		}
		if(lqls.length>1){
			condition = new ArrayList<String>();
			operation = new ArrayList<String>();
			String o = lqls[1].trim();
			String[] os = o.split(" ");
			String c="";
			for(String osItem : os){
				if(operationDic.contains(osItem)){
					operation.add(osItem);
					condition.add(c);
					c="";
				}else{
					c+=osItem;
				}
			}
			condition.add(c);
		}
	 
		info.setCondition(condition); 
		info.setOperation(operation);
		return info;
	}
 
	public static void main(String[] args) {
		getSQLInfo("from  Book,Style where Book.styleId=Style.styleId and  bookId=1");
	}
}
